package com.ruoyi.activiti.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.activiti.mapper.LeaveBeanMapper;
import com.ruoyi.activiti.domain.LeaveBean;
import com.ruoyi.activiti.service.ILeaveBeanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 请假Service业务层处理
 * 
 * @author qmc
 * @date 2025-07-18
 */
@Slf4j
@Service
public class LeaveBeanServiceImpl implements ILeaveBeanService {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private LeaveBeanMapper leaveBeanMapper;

    /**
     * 查询请假
     * 
     * @param id 请假主键
     * @return 请假
     */
    @Override
    public LeaveBean selectLeaveBeanById(Long id)
    {
        return leaveBeanMapper.selectLeaveBeanById(id);
    }

    /**
     * 查询请假列表
     * 
     * @param leaveBean 请假
     * @return 请假
     */
    @Override
    public List<LeaveBean> selectLeaveBeanList(LeaveBean leaveBean)
    {
        return leaveBeanMapper.selectLeaveBeanList(leaveBean);
    }

    /**
     * 新增请假
     * 
     * @param leaveBean 请假
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLeaveBean(LeaveBean leaveBean)
    {
        leaveBean.setCreateTime(DateUtils.getNowDate());
        leaveBean.setCreateBy(SecurityUtils.getUsername());
        int row = leaveBeanMapper.insertLeaveBean(leaveBean);
        ProcessInstance pr = runtimeService.createProcessInstanceBuilder()
                .processDefinitionKey("levave_1")
                .businessKey(leaveBean.getId().toString())
                .name("请假流程")
                .start();
        log.info("流程启动,id:{},业务key:{},实例id:{},定义ID:{},定义Key:{}",pr.getId(),pr.getBusinessKey(),
                pr.getProcessInstanceId(),pr.getProcessDefinitionId(),pr.getProcessDefinitionKey());
        leaveBean.setProcessIns(pr.getProcessInstanceId());
        leaveBean.setProcessDefId(pr.getProcessDefinitionId());
        leaveBeanMapper.updateLeaveBean(leaveBean);
        return row;
    }

    /**
     * 修改请假
     * 
     * @param leaveBean 请假
     * @return 结果
     */
    @Override
    public int updateLeaveBean(LeaveBean leaveBean)
    {
        return leaveBeanMapper.updateLeaveBean(leaveBean);
    }

    /**
     * 批量删除请假
     * 
     * @param ids 需要删除的请假主键
     * @return 结果
     */
    @Override
    public int deleteLeaveBeanByIds(Long[] ids)
    {
        return leaveBeanMapper.deleteLeaveBeanByIds(ids);
    }

    /**
     * 删除请假信息
     * 
     * @param id 请假主键
     * @return 结果
     */
    @Override
    public int deleteLeaveBeanById(Long id)
    {
        return leaveBeanMapper.deleteLeaveBeanById(id);
    }
}
